fix: resolve test hang with websockets 14+#3720
Closed
SamMorrowDrums wants to merge 1 commit intoencode:masterfrom
Closed
fix: resolve test hang with websockets 14+#3720SamMorrowDrums wants to merge 1 commit intoencode:masterfrom
SamMorrowDrums wants to merge 1 commit intoencode:masterfrom
Conversation
The test suite hangs after upgrading websockets from 13.x to 14.x/15.x because websockets 14.0 switched to a new asyncio implementation by default. Changes: 1. In TestServer.serve(): Replace deprecated asyncio.get_event_loop() pattern with asyncio.create_task() which is the modern way to create tasks when already inside an async context. 2. In serve_in_thread(): Instead of relying on server.run() which uses asyncio.run(), explicitly create a new event loop in the background thread with asyncio.new_event_loop() and asyncio.set_event_loop(). 3. Remove loop="asyncio" from Config() since we now manage the event loop manually. This approach is compatible with uvicorn 0.36+ (which removed the deprecated setup_event_loop() method) and works with websockets 14+. Fixes encode#3708
Contributor
|
Duplicate of #3693, but thank you very much for the contribution. I will mention you in the fix as a co author! |
Contributor
|
@SamMorrowDrums I applied this change on top of |
Contributor
|
When I uninstall |
Contributor
|
Please note that I use |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3708
The test suite hangs indefinitely when upgrading
websocketsfrom 13.x to 14.x or 15.x. This is because websockets 14.0 switched to a new asyncio implementation by default, which has different behavior around event loop handling.Root Cause
The issue stems from how the
TestServermanages event loops when running in a background thread:TestServer.serve(): The deprecatedasyncio.get_event_loop()+loop.create_task()pattern doesn't work correctly with websockets 14+'s new asyncio handlingserve_in_thread(): Theserver.run()method relies onasyncio.run()which doesn't play well with the threaded setup when websockets is involvedChanges
TestServer.serve(): Replace deprecatedasyncio.get_event_loop()pattern withasyncio.create_task()which is the modern way to create tasks when already inside an async context.serve_in_thread(): Instead of relying onserver.run()which usesasyncio.run(), explicitly create a new event loop in the background thread withasyncio.new_event_loop()andasyncio.set_event_loop().serverfixture: Removeloop="asyncio"fromConfig()since we now manage the event loop manually, avoiding conflicts with websockets' asyncio implementation.Testing
Tested locally with websockets 15.0.1 - all 43 integration tests pass without hanging.